package com.starryx.websocket.chatroom;

import com.starryx.websocket.chatroom.net.ChatroomServer;
import com.starryx.websocket.chatroom.net.config.NetConfig;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;

@Component
@Order(1)
public class ServerStarter implements ApplicationRunner {

    private static final Logger log = LogManager.getLogger(ServerStarter.class);

    @Autowired
    private NetConfig netConfig;

    private ChatroomServer chatroomServer;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        chatroomServer = new ChatroomServer(netConfig.getIp(), netConfig.getPort(), netConfig.getBossGroupThread(), netConfig.getWorkerGroupThread());
        chatroomServer.start();
        log.info("服务器启动成功，ip：{}，端口：{}", netConfig.getIp(), netConfig.getPort());
    }

    @PreDestroy
    public void destroy() {
        chatroomServer.shutdown();
    }
}
